<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="false">
			<block slot="content">{{i18n.login}}</block>
		</cu-custom>
		<view class="login-bg">
			<view class="login-card">
				<view class="login-head">{{i18n.inputUserTips}}</view>
				<view class="login-input login-margin-b">
					<input type="number" v-model="phone" :placeholder="i18n.phoneHint" />
				</view>
				<view class="login-input">
					<input type="password" v-model="password" :placeholder="i18n.inputPsdHint" />
				</view>
				<view class="login-function">
					<!-- <view class="login-forget" @click="go_forget">忘记密码</view> -->
					<view class="login-register" @click="go_register">{{i18n.actionReg}}</view>
				</view>
			</view>
		</view>
		<view class="login-btn">
			<button class="landing" @click="go_login()" type="primary">{{i18n.login}}</button>
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			i18n() {
				return this.$t('login')
			},
		},
		data() {
			return {
				phone: '',
				password: ''
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			go_forget() {
				uni.navigateTo({
					url: 'forget'
				})
			},
			go_register() {
				uni.navigateTo({
					url: 'register'
				})
			},
			go_login() {
				if (this.phone.length <= 5) {
					uni.showToast({
						title: this.i18n.phoneIncorrect,
						icon: 'none'
					})
					return
				}else if(!this.checkPhone(this.phone)){
					uni.showToast({
						title: this.i18n.phoneIncorrect,
						icon: 'none'
					})
					return
				}
				
				if (this.password.length <= 0) {
					uni.showToast({
						title: this.i18n.inputPsdHint,
						icon: 'none'
					})
					return
				}
				// console.log('phone=',this.phone)
				// console.log('password=',this.password)
				uni.request({
					url: this.$constant.BASEURL + this.$constant.PSD_LOGIN,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						phone: this.phone,
						pwd: this.password,
						type: 2
					},
					success: (res) => {
						var result = res.data
						console.log(result)
						switch (result.code) {
							//账号已绑定，但未选择公司
							case '0':
								this.go_company(result.data)
								break;
								//账号已绑定，且已经选择公司
							case '1':
								// console.log(result)
								this.saveUserInfo()
								getApp().globalData.unitno = result.data.departlist[0].unitno
								getApp().globalData.userid = result.data.departlist[0].userid
								this.$constant.saveUserid(result.data.departlist[0].userid)
								this.$constant.saveUnitno(result.data.departlist[0].unitno)
								this.go_index()
								break;
								//未绑定
							default:
								uni.showToast({
									title:result.message,
									icon:'none'
								})
								break;
						}
					}
				});
			},
			checkPhone(number) {
				// return /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(number)
				return true
			},
			go_index() {
				uni.switchTab({
					url: '../index/index'
				});
			},
			go_company(company) {
				uni.redirectTo({
					url: `../selectcompany/selectcompany?company=${JSON.stringify(company)}`
				})
			},
			saveUserInfo() {
				uni.setStorage({
					key: 'userinfo',
					data: {
						username: this.phone,
						password: this.password
					},
					success: function() {
						console.log('success');
					}
				});
			},
			getUserInfo() {
				var that = this
				uni.getStorage({
					key: 'userinfo',
					success: function(res) {
						var result = res.data
						that.phone = result.username
						that.password = result.password
					},
					fail() {
						console.log("getStorage error")
					}
				})
			}
		}
	}
</script>

<style>
	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #5599ff, #5599ff);
	}

	.login-btn {
		padding: 10upx 20upx;
		margin-top: 350upx;
	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: left;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: right;
		font-size: 26upx;

	}

	.login-input input {
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;
	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}

	.login-card {
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.12);
		position: relative;
		margin-top: 120upx;
	}

	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#5599ff, #5599ff);
	}
</style>
